package com.panopset.centralen.util;

import java.io.StringWriter;

/**
 *
 * Character utilities.
 *
 * @author Karl Dinwiddie
 *
 */
public final class CharUtil {

    /**
     * Main method for testing things only.
     *
     * @param s
     *            Not used.
     */
    public static void main(final String... s) {
        System.out.println(CharUtil.unicodeDump(" "));
    }

    /**
     * Obtain a unicode dump of a String.
     *
     * <h5>Reference</h5>
     * <ul>
     * <li>
     * <a href="http://java.sodeso.nl/java-development/java/"
     * >http://java.sodeso.nl/java-development/java/how-do-i-get-the-unicode-
     * value-of-a-character </a></li>
     * </ul>
     *
     * @param string
     *            String to dump in unicode representation.
     * @return Unicode representation of the given String.
     */
    private static String unicodeDump(final String string) {
        StringWriter sw = new StringWriter();

        for (int index = 0; index < string.length(); index++) {
            String hexCode = Integer.toHexString(string.codePointAt(index))
                    .toUpperCase();
            String hexCodeWithAllLeadingZeros = "0000" + hexCode;
            String hexCodeWithLeadingZeros = hexCodeWithAllLeadingZeros
                    .substring(hexCodeWithAllLeadingZeros.length()
                            - MathUtil.FOUR);

            System.out.println("\\u" + hexCodeWithLeadingZeros);
        }

        return sw.toString();
    }

    /**
     * Prevent instantiation.
     */
    private CharUtil() {
    }
}
